package com.study.ju.web;

import com.alibaba.fastjson.JSON;
import com.study.ju.entity.Book;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * Created by tao on 2018/7/21.
 */
@Controller
@RequestMapping("/test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/getMsg")
    @ResponseBody
    public String getMsg(HttpServletRequest request) {
        logger.info("查询所有1用户信息");
        return "sadasdas";
    }

    @RequestMapping("/testJson")
    @ResponseBody
    public Book testJsonFrom(@RequestBody Book req, HttpServletRequest request) throws IOException {
        logger.info("查询所有1用户信息" + JSON.toJSONString(req));
        String bodyParam = IOUtils.toString(request.getInputStream());
        System.out.println("testJson"+bodyParam);
        return req;
    }

    @RequestMapping("/testWwwFrom")
    @ResponseBody
    public Book testWwwFrom(Book req, HttpServletRequest request) {
        logger.info("查询所有1用户信息" + JSON.toJSONString(req));
        System.out.println("testWwwFrom2222:"+request.getParameter("formToken"));
        return req;
    }

    @RequestMapping("/testFormData")
    @ResponseBody
    public Book testFormData(Book req, HttpServletRequest request) {
        logger.info("查询所有1用户信息" + JSON.toJSONString(req));
        System.out.println("testFormData2222:"+request.getParameter("formToken"));
        return req;
    }

    @RequestMapping("/testRequestParam")
    @ResponseBody
    public String testRequestParam(@RequestParam String name, HttpServletRequest servletRequest) {
        logger.info("查询所有1用户信息" + JSON.toJSONString(name));
        return name;
    }
}
